package com.xiaochong.tir.manager.controller;



import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;

import com.xiaochong.service.common.config.oss.OSSTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.xiaochong.tir.manager.config.oss.OSSToolManage;
import com.xiaochong.tir.manager.util.KindEditorResponse;



/**
 * @ClassName: KindEditorController
 * @Description: TODO(ueditor 富文本编辑器上传文件类)
 * @author 杨松松
 * @date 2017年10月28日 下午1:58:23
 * 
 */
@Controller
@RequestMapping("/ueditor")
@Api("富文本编辑器上传文件")
public class KindEditorController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private OSSToolManage oSSToolManage;

	@Autowired
	private OSSTool ossTool;

	/** 
	 * 服务器统一请求上传接口路径
	* @Title: imgUploadConfig 
	* @Description: TODO() 
	* @param @param request
	* @param @return    
	* @return String    
	* @throws 
	*/
	@RequestMapping(value = "/imgUploadConfig")
	@ResponseBody
	public static String imgUploadConfig(HttpServletRequest request) {
		return "{\"imageActionName\":\"uploadimage\",\"imageFieldName\":\"upfile\","
				+ "\"imageMaxSize\":2048000,\"imageAllowFiles\":[\".png\",\".jpg\","
				+ "\".jpeg\",\".gif\",\".bmp\"],\"imageCompressEnable\":true,"
				+ "\"imageCompressBorder\":1600,\"imageInsertAlign\":\"none\","
				+ "\"imageUrlPrefix\":\"\",\"imagePathFormat\":\"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\""
				+ ",\"scrawlActionName\":\"uploadscrawl\",\"scrawlFieldName\":\"upfile\","
				+ "\"scrawlPathFormat\":\"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\","
				+ "\"scrawlMaxSize\":2048000,\"scrawlUrlPrefix\":\"\",\"scrawlInsertAlign\":\"none\","
				+ "\"snapscreenActionName\":\"uploadimage2\",\"snapscreenPathFormat\":\"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\""
				+ ",\"snapscreenUrlPrefix\":\"\",\"snapscreenInsertAlign\":\"none\","
				+ "\"catcherLocalDomain\":[\"127.0.0.1\",\"localhost\","
				+ "\"oss.renrenhua.com\",\"img.baidu.com\"],"
				+ "\"catcherActionName\":\"catchimage\","
				+ "\"catcherFieldName\":\"source\","
				+ "\"catcherPathFormat\":\"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\","
				+ "\"catcherUrlPrefix\":\"\",\"catcherMaxSize\":2048000,\"catcherAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],"
				+ "\"videoActionName\":\"uploadvideo\",\"videoFieldName\":\"upfile\",\"videoPathFormat\":\"/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}\","
				+ "\"videoUrlPrefix\":\"\",\"videoMaxSize\":102400000,\"videoAllowFiles\":[\".flv\",\".swf\","
				+ "\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\","
				+ "\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\"],\"fileActionName\":\"uploadfile\",\"fileFieldName\":\"upfile\","
				+ "\"filePathFormat\":\"/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}\",\"fileUrlPrefix\":\"\","
				+ "\"fileMaxSize\":51200000,\"fileAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\",\".flv\","
				+ "\".swf\",\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\","
				+ "\".webm\",\".mp3\",\".wav\",\".mid\",\".rar\",\".zip\",\".tar\",\".gz\",\".7z\",\".bz2\",\".cab\",\".iso\",\".doc\","
				+ "\".docx\",\".xls\",\".xlsx\",\".ppt\",\".pptx\",\".pdf\",\".txt\",\".md\",\".xml\"],\"imageManagerActionName\":\"listimage\","
				+ "\"imageManagerListPath\":\"/ueditor/jsp/upload/image/\",\"imageManagerListSize\":20,\"imageManagerUrlPrefix\":\"\","
				+ "\"imageManagerInsertAlign\":\"none\",\"imageManagerAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],"
				+ "\"fileManagerActionName\":\"listfile\",\"fileManagerListPath\":\"/ueditor/jsp/upload/file/\",\"fileManagerUrlPrefix\":\"\","
				+ "\"fileManagerListSize\":20,\"fileManagerAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\",\".flv\",\".swf\",\".mkv\","
				+ "\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\","
				+ "\".rar\",\".zip\",\".tar\",\".gz\",\".7z\",\".bz2\",\".cab\",\".iso\",\".doc\",\".docx\",\".xls\",\".xlsx\",\".ppt\",\".pptx\","
				+ "\".pdf\",\".txt\",\".md\",\".xml\"]}";
	}

	
	/** 
	* @Title: imgUpdate 
	* @Description: TODO(上传图片至OSS) 
	* @param @param file
	* @param @return    
	* @return ResponseBean    
	* @throws 
	*/
	@ApiOperation(value = "KindEditor上传图片",notes = "KindEditor上传图片")
	@RequestMapping(value = "/imgUpdate", produces = "application/json; charset=utf-8", method = RequestMethod.POST)
	@ResponseBody
	public KindEditorResponse imgUpdate(@RequestParam(value = "imgFile") MultipartFile file) {
		String prefix;
		String imgUrl;
		String fileName;
		try {
			if (file.isEmpty()) {
				return new KindEditorResponse(1, "上传文件不能空", "");
			}
			prefix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
			fileName = UUID.randomUUID().toString().replace("-", "") + "." + prefix;
			boolean flag = ossTool.upload(file.getInputStream(),fileName);
			if (flag) {
				imgUrl=ossTool.getOSSUrl( fileName );
				return new KindEditorResponse(0, "上传文件成功", imgUrl);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new KindEditorResponse(1, "文件上传失败", e.getLocalizedMessage());
		}
		return new KindEditorResponse(1, "文件上传失败", "");
	}
}
